//
//  NSString+NN.m
//  TempletProject
//
//  Created by lina on 15/11/4.
//  Copyright © 2015年 张 坤. All rights reserved.
//

#import "NSString+NN.h"

@implementation NSString (NN)

#pragma mark - 去掉字符串中的点并转化为整型
- (NSInteger)changeToInteger{
    
    if ([self rangeOfString:@"."].length == 0 ) {
        return [self integerValue];
    }
    NSArray *arr = [self componentsSeparatedByString:@"."];
    NSString *versionSt=@"";
    for (int i=0; i<arr.count; i++) {
        versionSt = [NSString stringWithFormat:@"%@%@",versionSt,arr[i]];
    }
    if ([arr count]==2) {
        versionSt = [NSString stringWithFormat:@"%@0",versionSt];
    }
    
    return [versionSt integerValue];
}

- (CGSize)sizeThatFits:(CGSize)size font:(UIFont *)font {
    CGSize textSize;
    if ([NSString instancesRespondToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        textSize = [self boundingRectWithSize:CGSizeMake(size.width, size.height)
                                      options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading |NSStringDrawingTruncatesLastVisibleLine |NSLineBreakByWordWrapping
                                   attributes:@{NSFontAttributeName:font}
                                      context:nil].size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        textSize = [self sizeWithFont:font
                    constrainedToSize:CGSizeMake(size.width, size.height)
                        lineBreakMode:NSLineBreakByWordWrapping];
#pragma clang diagnostic pop
    }
    return CGSizeMake(textSize.width, textSize.height);
}


- (NSString*)stringByAddingURLEncodedQueryDictionary:(NSDictionary*)query {
    NSMutableDictionary* encodedQuery = [NSMutableDictionary dictionaryWithCapacity:[query count]];
    
    for (NSString* key in [query keyEnumerator]) {
        NSParameterAssert([key respondsToSelector:@selector(URLEncoded)]);
        NSString* value = [query objectForKey:key];
        NSParameterAssert([value respondsToSelector:@selector(URLEncoded)]);
        [encodedQuery setValue:[value URLEncoded] forKey:[key URLEncoded]];
    }
    
    return [self stringByAddingQueryDictionary:encodedQuery];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)URLEncoded {
    CFStringRef cfUrlEncodedString = CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                             (CFStringRef)self,NULL,
                                                                             (CFStringRef)@"!#$%&'()*+,/:;=?@[]",
                                                                             kCFStringEncodingUTF8);
    
    NSString *urlEncoded = [NSString stringWithString:(__bridge NSString *)cfUrlEncodedString];
    CFRelease(cfUrlEncodedString);
    return urlEncoded;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)stringByAddingQueryDictionary:(NSDictionary*)query {
    NSMutableArray* pairs = [NSMutableArray array];
    for (NSString* key in [query keyEnumerator]) {
        NSString* value = [query objectForKey:key];
        value = [value stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
        value = [value stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
        NSString* pair = [NSString stringWithFormat:@"%@=%@", key, value];
        [pairs addObject:pair];
    }
    
    NSString* params = [pairs componentsJoinedByString:@"&"];
    if ([params length]==0) {
        return self;
    }
    if ([self rangeOfString:@"?"].location == NSNotFound) {
        return [self stringByAppendingFormat:@"?%@", params];
        
    } else {
        return [self stringByAppendingFormat:@"&%@", params];
    }
}

//卫生间共享数组(共用0、私用1)格式0,1(可选)
-(NSString *)bathroomSharedsStr{
    if ([NSObject isNilOrNull:self]) {
        return @"";
    }
    NSArray *ary=@[@"共用",@"私用"];
    return ary[[self intValue]];
}

//支持交流语言(0：中文 1：日文 2：英语 3：韩语 4：粤语)
-(NSString *)supportLanguagesStr{
    NSArray *languageArrary=@[NSLocalizedString(@"zhongwen",@"中文"),NSLocalizedString(@"riyu",@"日本语"),NSLocalizedString(@"yingyu",@"English")];
    NSArray *ary=[self componentsSeparatedByString:@","];
    if (ary.count==0) {
        if (self.length>0) {
            return self;
        }
        return @"";
    }
    NSString *languagesStr=@"";
    for (NSString *code in ary) {
        NSString *tempStr=@"";
        if ([code intValue]<languageArrary.count) {
            tempStr=languageArrary[[code intValue]];
            if (languagesStr.length>0) {
                languagesStr=[languagesStr stringByAppendingString:@"   "];
            }
            languagesStr=[languagesStr stringByAppendingString:tempStr];
        }
    }
    return languagesStr;
}

//餐饮类型 西餐0、合餐1、中餐2 格式 0,0,2 (可选)
-(NSArray *)cookingTypeAry{
    NSArray *cookingTypeArrary=@[NSLocalizedString(@"fkRoom_roomInfo_xican",@"西餐"),NSLocalizedString(@"fkRoom_roomInfo_heshi",@"和食"),NSLocalizedString(@"fkRoom_roomInfo_zhongcan",@"中餐")];
    NSArray *ary=[self componentsSeparatedByString:@","];
    NSMutableArray *tempCookingTypeArrary=[NSMutableArray array];
    for (NSString *code in ary) {
        NSString *tempStr=@"-";
        if ([code intValue]<ary.count) {
            tempStr=cookingTypeArrary[[code intValue]];
        }
        [tempCookingTypeArrary addObject:tempStr];
    }
    return tempCookingTypeArrary;
}

//餐饮价格列表 格式：成人早餐价格，成人午餐价格，成人晚餐价格，儿童早餐价格，儿童午餐价格，儿童晚餐价格 注：NULL项目代表该时间不提供用餐服务。 如果房费中包括餐费，那么此处设置为0；-1 -> 不提供餐食 0 -> 免费餐 > 0 -> 餐食价格 例子： (-1,-1,-1,-1,-1,-1) (可选)
//"500,-1,2000,500,-1,2000"
-(NSString *)restaurantMealsStr{
    if ([self isEqual:@"-1"]) {
        return NSLocalizedString(@"fkRoom_roomInfo_not_provided",@"不提供餐食");
    }else if ([self isEqual:@"0"]){
        return NSLocalizedString(@"fkRoom_roomInfo_provide",@"免费餐");
    }
    return [NSString stringWithFormat:@"CNY%@",self];
}

//-(NSString *)facilitiesStr{
//    NSDictionary    *facilitiesDic=@{@"FA000":@"浴巾毛巾",
//                                     @"FA001":@"牙刷牙膏",
//                                     @"FA002":@"洗发水浴液",
//                                     @"FA003":@"刮胡刀",
//                                     @"FA004":@"电视机",
//                                     @"FA005":@"有线电视",
//                                     @"FA006":@"洗面台",
//                                     @"FA007":@"空调",
//                                     @"FA008":@"有线网络",
//                                     @"FA009":@"无线网络",
//                                     @"FA010":@"厨房",
//                                     @"FA011":@"洗衣机",
//                                     @"FA012":@"烘干机",
//                                     @"FA013":@"吹风机",
//                                     @"FA014":@"灭火器"};
//    return [facilitiesDic objectForKey:self];
//}

-(NSString *)facilitiesImageStr{
    NSDictionary    *facilitiesDic=@{@"FA001":@"maojin",
                                     @"FA002":@"yagaoyashua",
                                     @"FA003":@"xifalu",
                                     @"FA004":@"tixudao",
                                     @"FA005":@"dianshiji",
                                     @"FA006":@"youxiandianshi",
                                     @"FA007":@"shuzhangtai",
                                     @"FA008":@"kongtiao",
                                     @"FA009":@"youxianwanghuo",
                                     @"FA010":@"WIFI",
                                     @"FA011":@"chufang",
                                     @"FA012":@"xiyiji",
                                     @"FA013":@"hongganji",
                                     @"FA014":@"chuifengji",
                                     @"FA015":@"miehuoqi"};
    return [facilitiesDic objectForKey:self];
}


//门票类型
-(NSString *)aticketTypeStr{
    NSArray *aticketTypeArrary=@[NSLocalizedString(@"fkPlay_youlechang",@"游乐场"),NSLocalizedString(@"fkPlay_zhanlanguan",@"展览馆"),NSLocalizedString(@"fkPlay_bowuguan",@"博物馆"),NSLocalizedString(@"fkPlay_tiyusaishi",@"体育赛事"),NSLocalizedString(@"fkPlay_xiquzongyi",@"戏曲综艺"),NSLocalizedString(@"fkPlay_yanchanghui",@"演唱会"),NSLocalizedString(@"fkPlay_yinyuehui",@"音乐会"),NSLocalizedString(@"fkPlay_dianying",@"电影")];
    NSArray *ary=[self componentsSeparatedByString:@","];
    if (ary.count==0) {
        if (self.length>0) {
            return self;
        }
        return @"";
    }
    return aticketTypeArrary[[self intValue]];
}

//邮编显示格式转换
-(NSString *)changeZipCodeStr{
    if (self.length>4) {
        NSString    *preStr=[self substringToIndex:self.length-4];
        NSString    *subStr=[self substringFromIndex:self.length-4];
        return [NSString stringWithFormat:@"%@-%@",preStr,subStr];
    }
    return self;
}

//时间格式转换
-(NSString *)changeCheckTime{
    if ([self intValue]==24) {
        return @"00:00AM";
    }
    if ([self intValue]>12) {//PM
        return [NSString stringWithFormat:@"%d:00PM",[self intValue]-12];
    }
    //AM
    return [NSString stringWithFormat:@"%d:00AM",[self intValue]];
}


#pragma mark - 去除字符串前后空格与回车
- (NSString*)trimmingCharacters{
    if (self) {
        //whitespaceAndNewlineCharacterSet 去除前后空格与回车
        //whitespaceCharacterSet 除前后空格
        return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    return @"";
}
@end
